jQuery.extend({
    formatFloat:function(src, pos){
        var num = parseFloat(src).toFixed(pos);
        num = num.toString().replace(/\$|\,/g,'');
        if(isNaN(num)) num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10) cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
        return (((sign)?'':'-') + num + '.' + cents);
    }
});
$(function () {
    $(document).on('keyup','.productCount',function () {
        var count=$(this).val();
        var ival=parseInt(count);
        var i=$(this).index('.productCount');
        if(!isNaN(ival)){
            $.ajax({
                type:'GET',
                url:'http://localhost:8080/web_war_exploded/autoUpdateCart?count='+count+'&index='+i,
                success:function (data) {
                    console.log('114');
                    var HTML='                    <tr class="head">\n' +
                        '                        <th>Item ID</th>\n' +
                        '                        <th>Product ID</th>\n' +
                        '                        <th>Description</th>\n' +
                        '                        <th>In Stock?</th>\n' +
                        '                        <th>Quantity</th>\n' +
                        '                        <th>List Price</th>\n' +
                        '                        <th>Total Cost</th>\n' +
                        '                        <th>&nbsp;</th>\n' +
                        '                    </tr>\n';
                    if(data.length === 0||data ===null){
                        HTML+='<tr>\n' +
                            '                            <td colspan="8"><b>Your cart is empty.</b></td>\n' +
                            '                        </tr>'
                    }
                    else {
                        for (var i = 0; i < data.length; i++) {
                            var src = $.formatFloat(data[i].item.listPrice, 2);
                            var src2 = $.formatFloat(data[i].total, 2);
                            var total = 0;
                            HTML += '<tr>';
                            HTML += '<td><a href="itemView?itemId=' + data[i].item.itemId +
                                '">'
                            HTML += data[i].item.itemId + '</a></td>';
                            HTML += '<td>';
                            HTML += data[i].item.product.productId;
                            HTML += '</td>';
                            HTML += '<td>';
                            if (data[i].item.attribute1 !== undefined) {
                                HTML += data[i].item.attribute1;
                            }
                            if (data[i].item.attribute2 !== undefined) {
                                HTML += data[i].item.attribute2;
                            }
                            if (data[i].item.attribute3 !== undefined) {
                                HTML += data[i].item.attribute3;
                            }
                            if (data[i].item.attribute4 !== undefined) {
                                HTML += data[i].item.attribute4;
                            }
                            if (data[i].item.attribute5 !== undefined) {
                                HTML += data[i].item.attribute5;
                            }
                            HTML += data[i].item.product.name;
                            HTML += '</td>';
                            HTML += '<td>';
                            HTML += data[i].inStock;
                            HTML += '</td>';
                            HTML += '<td> <input type="text" class="productCount" name="';
                            HTML += data[i].item.itemId;
                            HTML += '" value="';
                            HTML += data[i].quantity;
                            HTML += '"> </td>';
                            HTML += '<td class="Price"> $';
                            HTML += src;
                            HTML += '</td>';
                            HTML += '<td class="Total" name="Total" data-total="';
                            HTML += data[i].total;
                            HTML += '">$';
                            HTML += src2;
                            HTML += '</td>';
                            HTML += '<td> <a href="removeCartItem?workingItemId=';
                            HTML += data[i].item.itemId;
                            HTML += '" class="Button">Remove</a>';
                            HTML += '</td>';
                            HTML += '</tr>';
                        }
                        $('#autoUpdate').html(HTML);
                        $('.Total').each(function () {
                            total+=$(this).data('total');
                        })
                        var src3 = $.formatFloat(total, 2);
                        HTML += '<tr> <td colSpan="7">';
                        HTML += 'Sub Total: $';
                        HTML += src3;
                        HTML += '<input type="submit" style="background-color: #90b4d6;color: white;border: none;" value="Update Cart"></td>';
                        HTML += '<td>&nbsp;</td></tr>';
                        $('#autoUpdate').html(HTML);
                    }
                    $('#autoUpdate').show();
                },
                error:function (errorMsg){
                    console.log('1919')
                    console.log(errorMsg);
                }
            })
        }
    })
})